package ifrn.scartissue.client;

import ifrn.scartissue.common.utils.Validator;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import marauroa.client.BannedAddressException;
import marauroa.client.TimeoutException;
import marauroa.common.game.AccountResult;
import marauroa.common.net.InvalidVersionException;

public class LogonDialog extends JDialog {
	
	JTextField userField;
	JPasswordField passwordField;
	JTextField emailField;
	
	public LogonDialog() {
		createComponents();
	}
	
	private void createComponents() {
		
		userField = new JTextField();
		passwordField = new JPasswordField();
		emailField = new JTextField();
		
		JLabel userLabel = new JLabel("usuario");
		JLabel passwordLabel = new JLabel("senha");
		JLabel emailLabel = new JLabel("email");
		
		JButton submitButton = new JButton("cadastrar");
		
		submitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				createAccount();
			}
		});
		
		GridLayout layout = new GridLayout(4, 2);
		setLayout(layout);
		
		add(userLabel);
		add(userField);
		add(passwordLabel);
		add(passwordField);
		add(emailLabel);
		add(emailField);
		add(submitButton);
		
		pack();
	}
	
	private void createAccount() {
		
		String username = userField.getText();
		String password = passwordField.getText();
		String email = emailField.getText();
		
		if (username.isEmpty()) {
			alertError("Nome de usuário vazio");
			return;
		}
		if (password.isEmpty()) {
			alertError("Senha vazia");
			return;
		}
		if (email.isEmpty()) {
			alertError("Email vazio");
			return;
		}
		
		if (!Validator.validEmail(email)) {
			alertError("Email inválido");
			return;
		}
		
		
		MarauroaClient client = MarauroaClient.get();
		
		try {
			client.connect("localhost", 9418);
		} catch (IOException e) {
			alertError("Couldn't connect");
			return;
		}
		
		
		AccountResult result;
		try {
			result = client.createAccount(username, password, email);
			
			if (result.failed()) {
				switch (result.getResult()) {
				case FAILED_EMPTY_STRING:
					alert("Todos os campos devem ser preenchidos");
					break;
				case FAILED_PLAYER_EXISTS:
					alert("Jogador já existe");
					break;
				case FAILED_EXCEPTION:
					alert("Erro Desconhecido");
					break;
				default:
					break;
				}
			} else {
				alert("Cadastrado com sucesso");
				setVisible(false);
			}
			
		} catch (TimeoutException e) {
			alertError("Timeout");
		} catch (InvalidVersionException e) {
			alertError("Versão inválida");
		} catch (BannedAddressException e) {
			alertError("IP Banido");
		}
	}
	
	private void alert(String message) {
		JOptionPane.showMessageDialog(this, message);
	}
	
	private void alertError(String message) {
		JOptionPane.showMessageDialog(this, message, "Erro", JOptionPane.ERROR_MESSAGE);
	}	
}
